﻿using System.Collections.Specialized;

//Gallio and MbUnit Framework
using MbUnit.Framework;

//MoQ Framework  (this namespace includes Moq.Contrib)
using Moq;

namespace MockingBird.Simulator.FunctionalTests.EngineTests
{
    using MockingBird.Common.Infrastructure.Logging;
    //Project references
    using MockingBird.Simulator.Framework;
    using MockingBird.Simulator.Framework.Interfaces;
    using MockingBird.Simulator.FunctionalTests.Common;


    [TestFixture]
    public class RequestDistributionFixture
    {
        private ExecutionContext context;
        private NameValueCollection headers = new NameValueCollection();


        /// <summary>
        /// The MessageHandler should be called when a ProcessRequest method is executed.
        /// </summary>
        [Test]
        public void ExpectCallToMessageHandlerOnProcessRequest()
        {
            //Setup - data
            var mockLogger = new Mock<ILogger>();
            var mockDispenser = new Mock<IHandlerDispenser>();
            var mockMessageHandler = new Mock<IMessageHandler>();
            var mockContext = new Mock<IExecutionContext>();
            var distributor = new RequestDistributor(mockContext.Object, mockLogger.Object, mockDispenser.Object);
            distributor.MessageHandler = mockMessageHandler.Object;

            //setup - expectations
            mockMessageHandler.Expect(x => x.HandleMessage(mockContext.Object));
            mockDispenser.Expect(x => x.GetHandler(mockContext.Object)).Returns(mockMessageHandler.Object);

            //Call the process request method
            distributor.ProcessRequest();

            //verify the call to the logger
            mockMessageHandler.Verify();
        }

        /// <summary>
        /// This test invokes the request distributor for a Http message and then checks that the 
        /// distributor parses the Url information correctly and hands off a correctly formatted address
        /// to the handler dispenser
        /// </summary>
        [Test]
        public void ExpectThatTheUrlPassedToHandlerIsCorrectlyFormatted()
        {
            //Setup - data
            var mockLogger = new Mock<ILogger>();
            var mockDispenser = new Mock<IHandlerDispenser>();
            var mockMessageHandler = new Mock<IMessageHandler>();
            var mockContext = new Mock<IExecutionContext>();


            var distributor = new RequestDistributor(mockContext.Object, mockLogger.Object, mockDispenser.Object);
            distributor.MessageHandler = mockMessageHandler.Object;

            //setup expectations
            mockDispenser.Expect(x => x.GetHandler(mockContext.Object)).Returns(mockMessageHandler.Object);
            mockContext.Expect(x => x.EndpointFullName).Returns(TestHelper.DEFAULT_TARGET_URL);

            //Call the process request method
            distributor.ProcessRequest();

            //verify the call to the logger
            mockDispenser.Verify();


        }


    }
}
